//
//  UIColor+YHExtension.swift
//  Adorbee
//
//  Created by amovision on 2024/11/26.
//

import UIKit

public extension UIColor {
  
  static let label1 = UIColor.secondaryLabel
  
  static let label2 = UIColor.tertiaryLabel
  
  static let label3 = UIColor.quaternaryLabel
  
  /// 根据十六进制字符串生成UIColor
      /// - Parameters:
      ///   - hexString: 十六进制颜色字符串，支持格式：
      ///     - 6位："FFFFFF"（RGB）、"#FFFFFF"（带#）
      ///     - 8位："FFFFFFFF"（RGBA）、"#FFFFFFFF"（带#）
      ///   - alpha: 透明度（当hexString为6位时生效，8位格式会忽略此参数）
      convenience init(hexString: String, alpha: CGFloat = 1.0) {
          // 处理字符串：移除#号，转为大写
          var cleanedString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
          if cleanedString.hasPrefix("#") {
              cleanedString.removeFirst()
          }
          cleanedString = cleanedString.uppercased()
          
          // 检查长度是否合法（6位或8位）
          guard [6, 8].contains(cleanedString.count) else {
              // 非法格式时返回透明色（或根据需求改为默认色）
              self.init(red: 0, green: 0, blue: 0, alpha: 0)
              return
          }
          
          // 提取RGB和Alpha值
          var rgbValue: UInt64 = 0
          Scanner(string: cleanedString).scanHexInt64(&rgbValue)
          
          let red: CGFloat
          let green: CGFloat
          let blue: CGFloat
          let alphaComponent: CGFloat
          
          if cleanedString.count == 8 {
              // 8位格式：前2位是Alpha，后6位是RGB
              alphaComponent = CGFloat((rgbValue & 0xFF000000) >> 24) / 255.0
              red = CGFloat((rgbValue & 0x00FF0000) >> 16) / 255.0
              green = CGFloat((rgbValue & 0x0000FF00) >> 8) / 255.0
              blue = CGFloat(rgbValue & 0x000000FF) / 255.0
          } else {
              // 6位格式：使用传入的alpha值
              red = CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0
              green = CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0
              blue = CGFloat(rgbValue & 0x0000FF) / 255.0
              alphaComponent = alpha
          }
          
          self.init(red: red, green: green, blue: blue, alpha: alphaComponent)
      }
  
  func alpha(_ opacity: CGFloat) -> UIColor {
    withAlphaComponent(opacity)
  }

  static func black(_ a: Double) -> UIColor {
    UIColor(red: 0, green: 0, blue: 0, alpha: a / 10)
  }

  static func white(_ a: Double) -> UIColor {
    UIColor(red: 1, green: 1, blue: 1, alpha: a / 10)
  }

  static func rgb(_ r: Double, _ g: Double, _ b: Double, _ a: Double = 1.0) -> UIColor {
    UIColor(red: r / 255, green: g / 255, blue: b / 255, alpha: a)
  }

  static func hex(_ v: String, _ a: Double = 1.0) -> UIColor {
    UIColor(hexString: v, alpha: a)
  }
  
  /**
   * gray("a") == #aaaaaa
   * gray("f5") == #f5f5f5
   * gray(229.0) == rgb: 229, 229, 229
   */
  static func gray(_ v: Any, _ a: Double = 1.0) -> UIColor {
    if let v = v as? String {
      if v.count == 1 {
        UIColor(hexString: String(repeating: v, count: 6), alpha: a)
      } else if v.count == 2 {
        UIColor(hexString: String(repeating: v, count: 3), alpha: a)
      } else {
        .black
      }
    } else if let v: Double = Any2Float(v) {
      rgb(v, v, v, a)
    } else {
      .black
    }
  }

  /// 动态颜色
  /// - Parameters:
  ///   - light: 浅色模式颜色
  ///   - dark: 深色模式颜色
  /// - Returns: 动态颜色
  static func dynamic(_ light: UIColor, _ dark: UIColor) -> UIColor {
    UIColor.init { $0.userInterfaceStyle == .light ? light : dark }
  }
}
